using System;
using System.Data;
using System.Configuration;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

namespace SodaBlue.MembershipWeb
{
	public partial class Default : System.Web.UI.Page
	{
		protected void Page_Load(object sender, EventArgs e)
		{
			if (!Page.IsPostBack)
			{
				BindUserGrid();
				BindAvailableRoleList();

				// Insert our own stylesheet
				HtmlLink cssLink = new HtmlLink();
				cssLink.Href="~/Assets/sbMembership.css";
				cssLink.Attributes.Add("rel", "stylesheet");
				cssLink.Attributes.Add("type", "text/css");
				Header.Controls.Add(cssLink);
			}
		}

		protected void btnAddUserOK_Click(object sender, EventArgs e)
		{
			MembershipUser user = AddUser();
			BindUserGrid();
		}

		private void BindUserGrid()
		{
			gvMembers.DataSource = Membership.GetAllUsers();
			gvMembers.DataBind();
		}

		protected void gvMembers_RowCommand(object sender, GridViewCommandEventArgs e)
		{
			if (e.CommandName == "Delete")
			{
				int rowID = Int32.Parse(e.CommandArgument.ToString());

				GridViewRow gr = gvMembers.Rows[rowID];
				string user = ((LinkButton)gr.Cells[0].Controls[0]).Text;
				Membership.DeleteUser(user);
				BindUserGrid();
			}
			else if (e.CommandName == "MODIFYROLES")
			{
				int rowID = Int32.Parse(e.CommandArgument.ToString());

				GridViewRow row = gvMembers.Rows[rowID];
				string userName = ((LinkButton)row.Cells[0].Controls[0]).Text;
				ViewState["UserName"] = userName;
				BindRoleGrid();
				// We do this so we can identify which user we're adding roles to
				UserRolesModalPopupExtender.Show();
			}
		}

		protected void gvMembers_RowDeleting(object sender, GridViewDeleteEventArgs e)
		{

		}

		private MembershipUser AddUser()
		{
			MembershipUser user = Membership.CreateUser(txtUsername.Text, txtPassword.Text, txtEmail.Text);
			return user;
		}

		private void ResetAddUserForm()
		{
            txtUsername.Text = string.Empty;
            txtPassword.Text = string.Empty;
            txtPassword2.Text = string.Empty;
            txtEmail.Text = string.Empty;
		}

		protected void btnOkUserRoles_Click(object sender, EventArgs e)
		{
			UserRolesModalPopupExtender.Hide();
		}

		protected void btnAddNewUser_Click(object sender, EventArgs e)
		{
			ResetAddUserForm();
			NewUserModalPopupExtender.Show();
			ScriptManager.GetCurrent(this.Page).SetFocus(txtUsername);
		}

		private void BindAvailableRoleList()
		{
			ddlAvailableRoles.DataSource = Roles.GetAllRoles();
			ddlAvailableRoles.DataBind();
		}

		private void BindRoleGrid()
		{
			// userName is stored in Viewstate
			if (ViewState["UserName"] != null)
			{
				string userName = ViewState["UserName"].ToString();
				gvUserRoles.DataSource = Roles.GetRolesForUser(userName);
				gvUserRoles.DataBind();
			}
		}

		protected void gvUserRoles_RowCommand(object sender, GridViewCommandEventArgs e)
		{
			if (e.CommandName == "Delete")
			{
				try
				{
					lblUserRolesError.Text = "";
					string roleName = e.CommandArgument.ToString();
					string userName = ViewState["UserName"].ToString();
					Roles.RemoveUserFromRole(userName, roleName);
					BindRoleGrid();
				}
				catch (Exception ex)
				{
					lblUserRolesError.Text = ex.Message;
				}
			}
		}

		protected void gvUserRoles_RowDeleting(object sender, GridViewDeleteEventArgs e)
		{

		}

		protected void btnUserRoleAdd_Click(object sender, EventArgs e)
		{
				try
				{
					lblUserRolesError.Text = "";
					string roleName = ddlAvailableRoles.SelectedValue;
					string userName = ViewState["UserName"].ToString();
					Roles.AddUserToRole(userName, roleName);
					BindRoleGrid();
				}
				catch (Exception ex)
				{
					lblUserRolesError.Text = ex.Message;
				}
		}

        protected void btnAddNewRole_Click(object sender, EventArgs e)
        {
            txtRolename.Text = "";
            NewRoleModalPopupExtender.Show();
						ScriptManager.GetCurrent(this.Page).SetFocus(txtRolename);
        }

        protected void btnAddRoleOK_Click(object sender, EventArgs e)
        {
					try
					{
						Roles.CreateRole(txtRolename.Text);
						BindAvailableRoleList();
					}
					catch (Exception ex)
					{
						lblAddRoleError.Text = ex.Message;
					}
        }

	}
}

